<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Component;
use Illuminate\Support\Facades\DB;

class EditorController extends Controller
{
    public function show(Request $request)
    {
        $component = Component::with('ComponentTypes')->find($request->id);
        $component->demo_data = json_decode($component->demo_data);
        $component->component_tree = json_decode($component->component_tree);
        $component->preview_html = $this->formatPreviewHtml($request->id);
        return ['component' => $component ];
    }

    public function formatPreviewHtml($id)
    {
        $component = Component::find($id);
        $compiled_js = $component->compiled_js;
        $html = <<<HTML
    <!DOCTYPE html>
    <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    hello world, this is good
    <script>
    $compiled_js
    </script>
</body>
HTML;
        return $html;
    }
}
